﻿using System;
using System.Collections.ObjectModel;
using System.Threading;

using OpenQA.Selenium;

using NUnit.Framework;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class L050_BoostSearchingTypes {
		private IWebDriver driver;
		private String forumName;
		private const Int32 indexDelay = 15;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);
			driver.DisableAntiSpam();
			driver.EnableLucene(false, indexDelay);
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void BoostOnTypes() {
			String title = "Now we're going to test how boost works!";
			String content = "In version 0.7 topics are boosted if they are announcements and stickies, so we should find all 3 types, and they should be ordered in a certain way.";

			driver.LogOn(Constants.Users.Moderator.Email, Constants.Users.Moderator.Password);

			driver.CreateTopic(this.forumName, "Sticky", title + " sticky", content);
			driver.CreateTopic(this.forumName, title + " regular", content);
			driver.CreateTopic(this.forumName, "Announcement", title + " announcement", content);

			driver.LogOff();

			// Let the indexer index the new topics!
			driver.Wait(indexDelay * 3);

			driver.Search("boost", this.forumName);

			ReadOnlyCollection<IWebElement> results = driver.FindElements(By.ClassName("topictitle"));
			Assert.AreEqual(3, results.Count, String.Format("We expected 3 results, we have 3 topics with boost in the title! We only got {0}", results.Count));

			Assert.AreEqual(true, results[0].Text.Contains(" announcement"), "The announcement should be the first result, but the first result was: " + (results.Count >= 1 ? results[0].Text : ""));
			Assert.AreEqual(true, results[1].Text.Contains(" sticky"), "The sticky should be the second result, but the second result was: " + (results.Count >= 2 ? results[1].Text : ""));
			Assert.AreEqual(true, results[2].Text.Contains(" regular"), "The regular topic should the third result, but the third result was: " + (results.Count >= 3 ? results[2].Text : ""));
		}
	}
}